home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / examples / C / x17c.c < prev    next >
C/C++ Source or Header  |  1994-08-05  |  8KB  |  328 lines

  1. /* $Id: x17c.c,v 1.2 1994/08/05 09:28:42 mjl Exp $
  2.  * $Log: x17c.c,v $
  3.  * Revision 1.2  1994/08/05  09:28:42  mjl
  4.  * Put in an early bail-out since it's not working yet.
  5.  *
  6.  * Revision 1.1  1994/04/08  12:08:54  mjl
  7.  * Preliminary stab at a strip chart demo (doesn't work yet).
  8.  *
  9. */
  10.  
  11. /* Demonstration program for PLPLOT: */
  12.  
  13. /* Plots a simple stripchart.
  14.  * Eventually I want a really cool demo here: slowly evolving plots of
  15.  * say density and temperature.  These only need to get updated every so
  16.  * often.  And then at the bottom some strip charts of energy or such
  17.  * that are continually updated.
  18.  */
  19.  
  20. #include <plplot.h>
  21. #include <math.h>
  22.  
  23. /* Data declarations for stripcharts. */
  24.  
  25. typedef struct {
  26.     PLINT npts, nptsmax;
  27.     PLFLT *x, *y;
  28.     PLFLT xmin, xmax, ymin, ymax, xjump, xlen;
  29.  
  30.     char *xspec, *yspec, *labx, *laby, *labtop;
  31.     PLINT colbox, colline, collab;
  32. } PLStrip;
  33.  
  34. static int sid;                /* strip id number */
  35. static PLStrip *strip[100];        /* Array of pointers */
  36. static PLStrip *stripc;            /* current strip chart */
  37.  
  38. /* Create 1d stripchart */
  39.  
  40. void
  41. plstripc(PLINT *id, char *xspec, char *yspec,
  42.      PLFLT xmin, PLFLT xmax, PLFLT xjump, PLFLT ymin, PLFLT ymax,
  43.      PLINT colbox, PLINT colline, PLINT collab,
  44.      char *labx, char *laby, char *labtop);
  45.  
  46. /* Generates a complete stripchart plot.  */
  47.  
  48. void
  49. plstrip_gen(PLStrip *strip);
  50.  
  51. /* Add a point to a stripchart.  */
  52.  
  53. void
  54. plstripa(PLINT id, PLFLT x, PLFLT y);
  55.  
  56. /* Deletes and releases memory used by a stripchart.  */
  57.  
  58. void
  59. plstripd(PLINT id);
  60.  
  61. /* Miscellaneous */
  62.  
  63. #ifndef MAX
  64. #define MAX(a,b)    (((a) > (b)) ? (a) : (b))
  65. #endif
  66. #ifndef MIN
  67. #define MIN(a,b)    (((a) < (b)) ? (a) : (b))
  68. #endif
  69.  
  70. /*----------------------------------------------------------------------*\
  71.  * main program
  72. \*----------------------------------------------------------------------*/
  73.  
  74. int
  75. main(int argc, char *argv[])
  76. {
  77.     PLINT id, n, nsteps = 1000;
  78.     PLFLT y, ymin, ymax;
  79.     PLFLT t, tmin, tmax, tjump, dt;
  80.     PLINT colbox, collab, colline;
  81.  
  82.     fprintf(stderr, "Sorry, this demo not yet in working order\n");
  83.     exit(1);
  84.  
  85. /* plplot initialization */
  86. /* Parse and process command line arguments */
  87.  
  88.     (void) plParseInternalOpts(&argc, argv, PL_PARSE_FULL);
  89.  
  90.     plSetInternalOpt("db", "");
  91.     plSetInternalOpt("np", "");
  92.  
  93. /* Initialize plplot */
  94.  
  95.     plinit();
  96.  
  97. /* Create strip chart */
  98.  
  99. /* User sets up plot completely except for window and data 
  100.  * Eventually settings in place when strip chart is created will be
  101.  * remembered so that multiple strip charts can be used simultaneously.
  102.  */
  103.  
  104.     pladv(0);
  105.  
  106. /* For now just a standard viewport */
  107.  
  108.     plvsta();
  109.     plcol(4);
  110.  
  111. /* Specify some reasonable defaults for ymin and ymax */
  112. /* The plot will grow automatically if needed (but not shrink) */
  113.  
  114.     ymin = -1.0;
  115.     ymax =  1.0;
  116.  
  117. /* Specify initial tmin and tmax -- this determines length of window.
  118. /* Also specify maximum jump in t */
  119. /* This can accomodate adaptive timesteps */
  120.  
  121.     tmin = 0.;
  122.     tmax = 100.;
  123.     tjump = 25.;
  124.  
  125. /* Axes options same as plbox. */
  126. /* Only automatic tick generation and label placement allowed */
  127. /* Eventually I'll make this fancier */
  128.  
  129.     colbox = 1;
  130.     colline = 4;
  131.     collab = 3;
  132.  
  133.     plstripc(&id, "bcnst", "bcnstv",
  134.          tmin, tmax, tjump, ymin, ymax,
  135.          colbox, colline, collab,
  136.          "t", "d", "displacement vs time");
  137.  
  138. /* This is to represent a loop over time */
  139. /* Let's try a random walk process */
  140.  
  141.     y = 0.0;
  142.     dt = 0.1;
  143.     for (n = 0; n < nsteps; n++) {
  144.     t = n * dt;
  145.     y = y + rand();
  146.  
  147.     plstripa(id, t, y);
  148.     }
  149.  
  150. /* Destroy strip chart and it's memory */
  151.  
  152.     plstripd(id);
  153.  
  154.     plend();
  155.     exit(0);
  156. }
  157.  
  158. /*----------------------------------------------------------------------*\
  159.  * plstripc
  160.  *
  161.  * Create 1d stripchart.
  162. \*----------------------------------------------------------------------*/
  163.  
  164. void
  165. plstripc(PLINT *id, char *xspec, char *yspec,
  166.      PLFLT xmin, PLFLT xmax, PLFLT xjump, PLFLT ymin, PLFLT ymax,
  167.      PLINT colbox, PLINT colline, PLINT collab,
  168.      char *labx, char *laby, char *labtop)
  169. {
  170.     int i;
  171.  
  172. /* Get a free strip id and allocate it */
  173.  
  174.     for (i = 0; i < 100; i++) {
  175.     if (strip[i] == NULL)
  176.         break;
  177.     }
  178.  
  179.     if (i == 0) {
  180.     fprintf(stderr, "plstripc: Cannot create new strip chart\n");
  181.     *id = -1;
  182.     return;
  183.     }
  184.     else {
  185.     sid = *id = i;
  186.     strip[sid] = (PLStrip *) malloc((size_t) sizeof(PLStrip));
  187.     if (strip[sid] == NULL)
  188.         plexit("plstripc: Out of memory.");
  189.  
  190.     memset((char *) strip[sid], 0, sizeof(PLStrip));
  191.     }
  192.  
  193. /* Fill up the struct with all relevant info */
  194.  
  195.     stripc = strip[sid];
  196.  
  197.     stripc->npts = 0;
  198.     stripc->nptsmax = 100;
  199.     stripc->x = (PLFLT *) malloc((size_t) sizeof(PLFLT) * stripc->nptsmax);;
  200.     stripc->y = (PLFLT *) malloc((size_t) sizeof(PLFLT) * stripc->nptsmax);;
  201.     if (stripc->x == NULL || stripc->y == NULL)
  202.     plexit("plstripc: Out of memory.");
  203.  
  204.     stripc->xmin = xmin;
  205.     stripc->xmax = xmax;
  206.     stripc->ymin = ymin;
  207.     stripc->ymax = ymax;
  208.     stripc->xjump = xjump;
  209.     stripc->xlen = xmax - xmin;
  210.     stripc->xspec = xspec;
  211.     stripc->yspec = yspec;
  212.     stripc->labx = labx;
  213.     stripc->laby = laby;
  214.     stripc->labtop = labtop;
  215.     stripc->colbox = colbox;
  216.     stripc->colline = colline;
  217.     stripc->collab = collab;
  218.  
  219. /* Generate the plot */
  220.  
  221.     plstrip_gen(stripc);
  222. }
  223.  
  224. /*----------------------------------------------------------------------*\
  225.  * plstrip_gen
  226.  *
  227.  * Generates a complete stripchart plot.  Used either initially or
  228.  * during rescaling.
  229. \*----------------------------------------------------------------------*/
  230.  
  231. void
  232. plstrip_gen(PLStrip *strip)
  233. {
  234.  
  235. /* Set up window */
  236.  
  237.     plwind(strip->xmin, strip->xmax, strip->ymin, strip->ymax);
  238.  
  239. /* Draw box */
  240.  
  241.     plcol(strip->colbox);
  242.     plbox(strip->xspec, 0.0, 0, strip->yspec, 0.0, 0);
  243.  
  244.     plcol(strip->collab);
  245.     pllab(strip->labx, strip->laby, strip->labtop);
  246.  
  247.     if (strip->npts > 0) {
  248.     plcol(strip->colline);
  249.     plline(strip->npts, strip->x, strip->y);
  250.     }
  251. }
  252.  
  253. /*----------------------------------------------------------------------*\
  254.  * plstripa
  255.  *
  256.  * Add a point to a stripchart.  
  257.  * Allocates memory and rescales as necessary.
  258. \*----------------------------------------------------------------------*/
  259.  
  260. void
  261. plstripa(PLINT id, PLFLT x, PLFLT y)
  262. {
  263.     int i, istart;
  264.  
  265.     stripc = strip[id];
  266.  
  267. /* Add new point, allocating memory if necessary */
  268.  
  269.     if (++stripc->npts > stripc->nptsmax) {
  270.     stripc->nptsmax += 32;
  271.     stripc->x = (PLFLT *) realloc((void *) stripc->x, stripc->nptsmax);
  272.     stripc->y = (PLFLT *) realloc((void *) stripc->y, stripc->nptsmax);
  273.     }
  274.     stripc->x[stripc->npts-1] = x;
  275.     stripc->y[stripc->npts-1] = y;
  276.  
  277.     stripc->xmax = x;
  278.     stripc->ymax = MAX(y, stripc->ymax);
  279.     stripc->ymin = MIN(y, stripc->ymin);
  280.  
  281. /* Now either plot new point or regenerate plot */
  282.  
  283.     if (stripc->xmax - stripc->xmin < stripc->xlen) {
  284.     plP_movwor(stripc->x[stripc->npts-2], stripc->y[stripc->npts-2]);
  285.     plP_drawor(stripc->x[stripc->npts-1], stripc->y[stripc->npts-1]);
  286.     }
  287.     else {
  288.  
  289. /* Regenerating plot */
  290. /* First push back the x scale */
  291.  
  292.     stripc->xmin -= stripc->xjump;
  293.     stripc->xmax -= stripc->xjump;
  294.     istart = 0;
  295.     while (stripc->x[istart] - stripc->xjump < 0.)
  296.         istart++;
  297.  
  298.     stripc->npts -= istart;
  299.     for (i = 0; i < stripc->npts; i++) {
  300.         stripc->x[i] = stripc->x[i+istart];
  301.         stripc->y[i] = stripc->y[i+istart];
  302.     }
  303.  
  304.     stripc->xmin = stripc->x[0];
  305.     stripc->xmax = stripc->x[stripc->npts];
  306.  
  307. /* Now do the real work */
  308.  
  309.     plstrip_gen(stripc);
  310.     }
  311. }
  312.  
  313. /*----------------------------------------------------------------------*\
  314.  * plstripd
  315.  *
  316.  * Deletes and releases memory used by a stripchart.  
  317. \*----------------------------------------------------------------------*/
  318.  
  319. void
  320. plstripd(PLINT id)
  321. {
  322.     stripc = strip[id];
  323.  
  324.     free((void *) stripc->x);
  325.     free((void *) stripc->y);
  326.     free((void *) stripc);
  327. }
  328.